<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/src/qt3support/tools/q3deepcopy.cpp -->
<head>
  <title>Qt 4.3: Q3DeepCopy Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Q3DeepCopy Class Reference<br /><sup><sup>[<a href="qt3support.html">Qt3Support</a> module]</sup></sup></h1>
<p>The Q3DeepCopy class is a template class which ensures that implicitly shared and explicitly shared classes reference unique data. <a href="#details">More...</a></p>
<pre> #include &lt;Q3DeepCopy&gt;</pre><p><b>This class is part of the Qt 3 support library.</b> It is provided to keep old source code working. We strongly advise against using it in new code. See <a href="porting4.html">Porting to Qt 4</a> for more information.</p>
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
<ul>
<li><a href="q3deepcopy-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h3>Public Functions</h3>
<ul>
<li><div class="fn"/><b><a href="q3deepcopy.html#Q3DeepCopy">Q3DeepCopy</a></b> ()</li>
<li><div class="fn"/><b><a href="q3deepcopy.html#Q3DeepCopy-2">Q3DeepCopy</a></b> ( const T &amp; <i>t</i> )</li>
<li><div class="fn"/><b><a href="q3deepcopy.html#operator-T">operator T</a></b> ()</li>
<li><div class="fn"/>Q3DeepCopy&lt;T&gt; &amp; <b><a href="q3deepcopy.html#operator-eq">operator=</a></b> ( const T &amp; <i>t</i> )</li>
</ul>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The Q3DeepCopy class is a template class which ensures that implicitly shared and explicitly shared classes reference unique data.</p>
<p>Normally, shared copies reference the same data to optimize memory use and for maximum speed. In the example below, <tt>s1</tt>, <tt>s2</tt>, <tt>s3</tt>, <tt>s4</tt> and <tt>s5</tt> share data.</p>
<pre><span class="comment"> //</span> all 5 strings share the same data
 QString s1 = &quot;abcd&quot;;
 QString s2 = s1;
 QString s3 = s2;
 QString s4 = s3;
 QString s5 = s2;</pre>
<p>Q3DeepCopy can be used several ways to ensure that an object references unique, unshared data. In the example below, <tt>s1</tt>, <tt>s2</tt> and <tt>s5</tt> share data, while neither <tt>s3</tt> nor <tt>s4</tt> share data.</p>
<pre><span class="comment"> //</span> s1, s2 and s5 share the same data, neither s3 nor s4 are shared
 QString s1 = &quot;abcd&quot;;
 QString s2 = s1;
 Q3DeepCopy&lt;QString&gt; s3 = s2;  <span class="comment">//</span> s3 is a deep copy of s2
 QString s4 = s3;             <span class="comment">//</span> s4 is a deep copy of s3
 QString s5 = s2;</pre>
<p>In the example below, <tt>s1</tt>, <tt>s2</tt> and <tt>s5</tt> share data, and <tt>s3</tt> and <tt>s4</tt> share data.</p>
<pre><span class="comment"> //</span> s1, s2 and s5 share the same data, s3 and s4 share the same data
 QString s1 = &quot;abcd&quot;;
 QString s2 = s1;
 QString s3 = Q3DeepCopy&lt;QString&gt;( s2 );  <span class="comment">//</span> s3 is a deep copy of s2
 QString s4 = s3;                        <span class="comment">//</span> s4 is a shallow copy of s3
 QString s5 = s2;</pre>
<p>Q3DeepCopy can also provide safety in multithreaded applications that use shared classes. In the example below, the variable <tt>global_string</tt> is used safely since the data contained in <tt>global_string</tt> is always a deep copy. This ensures that all threads get a unique copy of the data, and that any assignments to <tt>global_string</tt> will result in a deep copy.</p>
<pre> Q3DeepCopy&lt;QString&gt; global_string;  <span class="comment">//</span> global string data
 QMutex global_mutex;               <span class="comment">//</span> mutex to protext global_string

 ...

 void setGlobalString( const QString &amp;str )
 {
     global_mutex.lock();
     global_string = str;           <span class="comment">//</span> global_string is a deep copy of str
     global_mutex.unlock();
 }

 ...

 void MyThread::run()
 {
     global_mutex.lock();
     QString str = global_string;          <span class="comment">//</span> str is a deep copy of global_string
     global_mutex.unlock();

     <span class="comment">//</span> process the string data
     ...

     <span class="comment">//</span> update global_string
     setGlobalString( str );
 }</pre>
<p><b>Warning:</b> It is the application developer's responsibility to protect the object shared across multiple threads.</p>
<p>The examples above use <a href="qstring.html">QString</a>, which is an implicitly shared class. The behavior of Q3DeepCopy is the same when using explicitly shared classes like <a href="qbytearray.html">QByteArray</a>.</p>
<p>Currently, Q3DeepCopy works with the following classes:</p>
<ul>
<li>QMemArray (including subclasses like <a href="qbytearray.html">QByteArray</a> and <a href="porting4.html#qcstring">QCString</a>)</li>
<li><a href="qmap.html">QMap</a></li>
<li><a href="qstring.html">QString</a></li>
<li>QValueList (including subclasses like <a href="qstringlist.html">QStringList</a> and QValueStack)</li>
<li>QValueVector</li>
</ul>
<p>See also <a href="threads.html">Thread Support in Qt</a>.</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="Q3DeepCopy"></a>Q3DeepCopy::Q3DeepCopy ()</h3>
<p>Constructs an empty instance of type <i>T</i>.</p>
<h3 class="fn"><a name="Q3DeepCopy-2"></a>Q3DeepCopy::Q3DeepCopy ( const T &amp; <i>t</i> )</h3>
<p>Constructs a deep copy of <i>t</i>.</p>
<h3 class="fn"><a name="operator-T"></a>Q3DeepCopy::operator T ()</h3>
<p>Returns a deep copy of the encapsulated data.</p>
<h3 class="fn"><a name="operator-eq"></a>Q3DeepCopy&lt;T&gt; &amp; Q3DeepCopy::operator= ( const T &amp; <i>t</i> )</h3>
<p>Assigns a deep copy of <i>t</i>.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
